<?php
namespace app\stores\model\member;
use app\beautiful\model\LevelModel;
use app\beautiful\model\member\MemberBalanceModel;
use app\beautiful\model\member\MemberCouponModel;
use app\beautiful\model\PartnerModel;
use app\common\model\CommonModel;
use app\common\model\ShopModel;

class MemberLevelModel extends CommonModel{
    protected  $name = 'beautiful_member_level';
    protected  $pk   = 'member_level_id';

    /**
     * 模型写入前事件（钩子函数）
     */
    public static function onBeforeInsert($model)
    {
        $model->set("add_time", date('Y-m-d H:i:s'));
        $model->set("add_ip", request()->ip());
    }
    public function getById($where,$field='*')
    {
        $list = $this->where($where)->field($field)->find();
        if($list) $list = $list->toArray();

        return $list;
    }
    public function getLevel($where,$field='*')
    {
        $list = $this->alias('ml')
            ->join('beautiful_level l','ml.level_id=l.level_id')
            ->where(['ml.is_delete'=>0,'l.is_delete'=>0])
            ->where($where)
            ->field($field)
            ->find();

        return $list;
    }
    public function getList($where,$order,$page=0,$limit=0){
        $query = $this->alias('ml')
            ->join('shop s','ml.shop_id=s.shop_id')
            ->join('shop_detail sd','ml.shop_id=sd.shop_id')
            ->join('beautiful_level_setting ls','ml.shop_id=ls.shop_id and ls.is_selected=1')
            ->join('beautiful_level l','ml.level_id=l.level_id and l.type=ls.type')
            ->when(!empty($where['member_id']),function ($query) use ($where){
                $query->where(['ml.member_id'=>$where['member_id']]);
            })->when(!empty($where['shop_ids']),function ($query) use ($where){
                $query->whereIn('ml.shop_id',$where['shop_ids']);
            })->where(['ml.is_delete'=>0,'l.is_delete'=>0])
            ->where('ml.level_id','>',0)
            ->where('l.discount_force','>',0)
            ->field("ml.shop_id,ml.level_id,s.name as shop_name,sd.logo,l.name as level_name,l.discount_force");
        $count = $query->count();
        $query = $query->order($order);
        if($page>0) $query = $query->page($page);
        if($limit>0) $query = $query->limit($limit);
        $list = $query->select()->toArray();
        if(!empty($list)){
            foreach ($list as $k=>$v){
            }
        }
        return compact('count','list');
    }

    public static function list($where,$order,$page=0,$limit=0,$field='*'){
        $query = self::alias('ml')
            ->join('beautiful_member m','m.member_id=ml.member_id')
            ->join('shop s','ml.shop_id=s.shop_id')
            ->join('shop_detail sd','ml.shop_id=sd.shop_id')
            ->join('beautiful_level_setting ls','ml.shop_id=ls.shop_id and ls.is_selected=1')
            ->leftjoin('beautiful_level l','ml.level_id=l.level_id and l.type=ls.type')
            ->when(isset($where['search']),function ($query) use ($where){
                $query->where('m.mobile|m.member_num','like','%'.$where['search'].'%');
            })->when(isset($where['shop_ids']),function ($query) use ($where){
                $query->whereIn('ml.shop_id',$where['shop_ids']);
            })->where(['ml.is_delete'=>0])
            ->field($field);
        $count = $query->count();
        $query = $query->order($order)->group('ml.member_id');
        if($page>0) $query = $query->page($page);
        if($limit>0) $query = $query->limit($limit);
        $list = $query->select()->toArray();
        if(!empty($list)){
            foreach ($list as $k=>$v){
                $list[$k]['level_name'] = $v['level_name']??'普通会员';
                $list[$k]['discount'] = $v['type']==1?$v['discount']:$v['discount_force'];
                //股东
                $p_where['shop_id'] = $v['shop_id'];
                $p_where['member_id'] = $v['member_id'];
                $p_where['status'] = 1;
                $p_where['is_delete'] = 0;
                $partner = PartnerModel::info($p_where);
                $list[$k]['partner_type'] = $partner?$partner['type']:null;

                //是否是会员
                $mL_model = new MemberLevelModel();
                $ml = $mL_model->getById(['member_id'=>$v['member_id'],'shop_id'=>$v['shop_id'],'is_delete'=>0]);
                $list[$k]['is_vip'] = $ml?1:0;

                //余额
                $list[$k]['balance'] = MemberBalanceModel::where([

                        ['shop_id','=',$v['shop_id']],

                        ['member_id','=',$v['member_id']],

                        ['valid_bg_time','<=',time()]

                    ])->sum('remain_balance') ?? 0;
            }
        }
        return compact('count','list');
    }
}
